package dt.member;

import java.io.IOException;
import java.io.Reader;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class memberLoginAction extends ActionSupport implements SessionAware {
	
	private Map session;
	public static Reader reader;  // 占쏙옙占쏙옙 占쏙옙트占쏙옙占쏙옙 占쏙옙占쏙옙 reader
	public static SqlMapClient sqlMapper; // SqlMapClient API占쏙옙 占쏙옙占쏙옙歐占�占쏙옙占쏙옙 sqlMapper 占쏙옙체

	private memberVO paramClass;  // 占식띰옙占쏙옙拷占�占쏙옙占쏙옙占쏙옙 占쏙옙체
	private memberVO resultClass; // 占쏙옙占쏙옙 占쏙옙占�占쏙옙占쏙옙 占쏙옙占쏙옙占쏙옙 占쏙옙체
	
	private int pno;
	
	private String id;
	private String passwd;
	private String BoardFlag;
	private int loginCheck;
	
	//占쏙옙占쏙옙
	public memberLoginAction() throws IOException {
		reader = Resources.getResourceAsReader("sqlMapConfig.xml"); //sqlMapConfig.xml 占쏙옙占쏙옙占쏙옙 占쏙옙占쏙옙占쏙옙占쏙옙占쏙옙 占쏙옙占쏙옙占승댐옙.
		sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader); //sqlMapConfig.xml 占쏙옙占쏙옙占쏙옙 占쏙옙占쏙옙占�sqlMapper 占쏙옙체 占쏙옙
		reader.close();
	}
	
	public String form() {
		//System.out.println(BoardFlag);
		return SUCCESS;
	}
	
	//占싸깍옙占쏙옙 占쌓쇽옙
	public String execute() throws Exception {
		//System.out.println(BoardFlag);
		// 占식띰옙占쏙옙沽占�占쏙옙占쏙옙트 占쏙옙체 占쏙옙
		paramClass = new memberVO();
		resultClass = new memberVO();
		
		resultClass = (memberVO) sqlMapper.queryForObject("getMember", getId());
				
		if(resultClass == null){
			loginCheck = 0;	//占쏙옙占싱듸옙 占쏙옙占쏙옙
			return INPUT;
		}
		else {
			if(resultClass.getPasswd().equals(passwd)) {
				loginCheck = 1;	//占쏙옙占싱듸옙 占쌍곤옙 占싻쏙옙占쏙옙占쏙옙 占쏙옙치
				ActionContext context=ActionContext.getContext();	// 占쏙옙트占쏙옙占쏙옙2占쏙옙占쏙옙占쏙옙 占쏙옙占쏙옙 占쏙옙占쏙옙占쏙옙
				//Map<String,Object> session = context.getSession(); // ActionContext占쏙옙 占쏙옙占�占쌨억옙 占쏙옙占실깍옙占쏙옙
				session.put("id",id);
				session.put("nick", resultClass.getNickname());

				return SUCCESS;
			}
			else {
				
				loginCheck = -1;	//占싻쏙옙占쏙옙占썲가 占싫몌옙占쏙옙占쏙옙
				return INPUT;
			}
		}
	}

	public static Reader getReader() {
		return reader;
	}

	public static void setReader(Reader reader) {
		memberLoginAction.reader = reader;
	}

	public static SqlMapClient getSqlMapper() {
		return sqlMapper;
	}

	public static void setSqlMapper(SqlMapClient sqlMapper) {
		memberLoginAction.sqlMapper = sqlMapper;
	}

	public memberVO getParamClass() {
		return paramClass;
	}

	public void setParamClass(memberVO paramClass) {
		this.paramClass = paramClass;
	}

	public memberVO getResultClass() {
		return resultClass;
	}

	public void setResultClass(memberVO resultClass) {
		this.resultClass = resultClass;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public int getLoginCheck() {
		return loginCheck;
	}

	public void setLoginCheck(int loginCheck) {
		this.loginCheck = loginCheck;
	}

	public int getPno() {
		return pno;
	}

	public void setPno(int pno) {
		this.pno = pno;
	}
	
	@Override
	public void setSession(Map session) {
		this.session = session;		
	}
	
	public Map getSession(){
		return session;
	}
	
	public String getBoardFlag(){
		return BoardFlag;
	}
	
	public void setBoardFlag(String BoardFlag){
		this.BoardFlag = BoardFlag;
	}

}